﻿using System;
using System.Data;
using System.Configuration;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;


namespace ERPWebDemos.Code
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:Controltest runat=server></{0}:Controltest>")]
    public class Controltest : CompositeControl, INamingContainer
    {
        #region 声明要显示的控件
        public Label tableLabel;
        public Label fieldLabel;
        public DropDownList fieldComboBox;
        public TextBox fieldTextBox;
        #endregion
        #region 构造方法
        public Controltest()
            : base()
        {

        }
        #endregion
        #region 显示方法
        //撰写
        protected override void CreateChildControls()
        {
            this.tableLabel = new Label();
            this.fieldLabel = new Label();
            this.fieldComboBox = new DropDownList();
            this.fieldTextBox = new TextBox();

            this.tableLabel.Text = "tableLabel ";
            this.fieldLabel.Text = "fieldLabel ";

            fieldComboBox.Items.Add("1");
            fieldComboBox.Items.Add("2");
            fieldComboBox.Items.Add("3");

            if (!Page.IsPostBack)
            {
                fieldTextBox.Text = "dd";
            }

            Controls.Clear();
            this.Controls.Add(this.tableLabel);
            this.Controls.Add(this.fieldLabel);
            this.Controls.Add(this.fieldComboBox);
            this.Controls.Add(this.fieldTextBox);
            ChildControlsCreated = true;
        }
        //布局
        protected override void Render(HtmlTextWriter writer)
        {
            EnsureChildControls();

            AddAttributesToRender(writer);

            writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding,
                "1", false);

            writer.RenderBeginTag(HtmlTextWriterTag.Table);
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);

            tableLabel.RenderControl(writer);
            fieldLabel.RenderControl(writer);
            fieldComboBox.RenderControl(writer);
            fieldTextBox.RenderControl(writer);

            writer.RenderEndTag();  // Td
            writer.RenderEndTag();  // Tr
            writer.RenderEndTag();  // Table
        }
        #endregion
        #region 外部方法
        public string Text
        {
            get
            {
                return fieldTextBox.Text;
            }

            set
            {
                fieldTextBox.Text= value;
            }
        }
        #endregion
    }
}
